package com.ruoyi.common.utils;

import cn.hutool.core.map.BiMap;
import cn.hutool.core.util.StrUtil;

import java.util.HashMap;

/**
 * 中国国家民族
 * @author dazer
 */
public class ChineseNationalityCodeUtil {
    /**
     * 中国民族-相关码表 chinese nationality code
     */
    private static final BiMap<Integer, String> CHINESE_NATIONALITY_CODE = new BiMap<>(new HashMap<>());

    static {
        //民族代码表 56个民族
        // http://www.nbyz.gov.cn/art/2020/9/27/art_1229107563_58678611.html
        // http://www.wlht.gov.cn/mcxam/xxgk30/cyzw94/tjxx83/3735536/index.html
        CHINESE_NATIONALITY_CODE.put(1, "汉族");
        CHINESE_NATIONALITY_CODE.put(2, "蒙族");
        CHINESE_NATIONALITY_CODE.put(3, "回族");
        CHINESE_NATIONALITY_CODE.put(4, "藏族");
        CHINESE_NATIONALITY_CODE.put(5, "维吾尔族");
        CHINESE_NATIONALITY_CODE.put(6, "苗族");
        CHINESE_NATIONALITY_CODE.put(7, "彝族");
        CHINESE_NATIONALITY_CODE.put(8, "壮族");
        CHINESE_NATIONALITY_CODE.put(9, "布依族");
        CHINESE_NATIONALITY_CODE.put(10, "朝鲜族");
        CHINESE_NATIONALITY_CODE.put(11, "满族");
        CHINESE_NATIONALITY_CODE.put(12, "侗族");
        CHINESE_NATIONALITY_CODE.put(13, "瑶族");
        CHINESE_NATIONALITY_CODE.put(14, "白族");
        CHINESE_NATIONALITY_CODE.put(15, "土家族");
        CHINESE_NATIONALITY_CODE.put(16, "哈尼族");
        CHINESE_NATIONALITY_CODE.put(17, "哈萨克族");
        CHINESE_NATIONALITY_CODE.put(18, "傣族");
        CHINESE_NATIONALITY_CODE.put(19, "黎族");
        CHINESE_NATIONALITY_CODE.put(20, "傈僳族");
        CHINESE_NATIONALITY_CODE.put(21, "佤族");
        CHINESE_NATIONALITY_CODE.put(22, "畲族");
        CHINESE_NATIONALITY_CODE.put(23, "高山族");
        CHINESE_NATIONALITY_CODE.put(24, "拉祜族");
        CHINESE_NATIONALITY_CODE.put(25, "水族");
        CHINESE_NATIONALITY_CODE.put(26, "东乡族");
        CHINESE_NATIONALITY_CODE.put(27, "纳西族");
        CHINESE_NATIONALITY_CODE.put(28, "景颇族");
        CHINESE_NATIONALITY_CODE.put(29, "柯尔克孜族");
        CHINESE_NATIONALITY_CODE.put(30, "撒拉族");
        CHINESE_NATIONALITY_CODE.put(31, "达斡尔族");
        CHINESE_NATIONALITY_CODE.put(32, "仫佬族");
        CHINESE_NATIONALITY_CODE.put(33, "羌族");
        CHINESE_NATIONALITY_CODE.put(34, "布朗族");
        CHINESE_NATIONALITY_CODE.put(35, "撒拉族");
        CHINESE_NATIONALITY_CODE.put(36, "毛难族");
        CHINESE_NATIONALITY_CODE.put(37, "仡佬族");
        CHINESE_NATIONALITY_CODE.put(38, "锡伯族");
        CHINESE_NATIONALITY_CODE.put(39, "阿昌族");
        CHINESE_NATIONALITY_CODE.put(40, "普米族");
        CHINESE_NATIONALITY_CODE.put(41, "塔吉克族");
        CHINESE_NATIONALITY_CODE.put(42, "怒族");
        CHINESE_NATIONALITY_CODE.put(43, "乌孜别克族");
        CHINESE_NATIONALITY_CODE.put(44, "俄罗斯族");
        CHINESE_NATIONALITY_CODE.put(45, "鄂温克族");
        CHINESE_NATIONALITY_CODE.put(46, "崩龙族");
        CHINESE_NATIONALITY_CODE.put(47, "保安族");
        CHINESE_NATIONALITY_CODE.put(48, "裕固族");
        CHINESE_NATIONALITY_CODE.put(49, "京族");
        CHINESE_NATIONALITY_CODE.put(50, "塔塔尔族");
        CHINESE_NATIONALITY_CODE.put(51, "独龙族");
        CHINESE_NATIONALITY_CODE.put(52, "鄂伦春族");
        CHINESE_NATIONALITY_CODE.put(53, "德昂族");
        CHINESE_NATIONALITY_CODE.put(54, "门巴族");
        CHINESE_NATIONALITY_CODE.put(55, "珞巴族");
        CHINESE_NATIONALITY_CODE.put(56, "基诺族");
        // 其他  <====> 未定族称人口
        CHINESE_NATIONALITY_CODE.put(97, "其他");
        // 外国血统 <====> 外国血统中国籍人士 <===> 入籍
        CHINESE_NATIONALITY_CODE.put(98, "外国血统");
    }


    // -----------------------------------------------------------------------------------  民族 nationality start
    /**
     * 根据名族代码获取民族名称；
     * @param nationalCode 民族代码，取值：1-56、97、98
     * @return 民族名称
     * @author dazer
     * @since 5.6.4
     */
    public static String getNationalityName(int nationalCode) {
        return CHINESE_NATIONALITY_CODE.get(nationalCode);
    }


    /**
     * 根据民族名称 获取编码；
     * 民族：可以通过身份证读卡器获取到用户的民族名称，我们通过民族名称反查国家民族代码
     * @param nationalName 民族名称,eg: 汉族 或者 汉
     * @return 民族编码
     * @author dazer
     * @since 5.6.4
     */
    public static Integer getNationalityCode(String nationalName) {
        if (nationalName == null || nationalName.length() <= 0) {
            return null;
        }
        Integer code =  CHINESE_NATIONALITY_CODE.getKey(nationalName);
        // 补偿方案1，存在多种叫法
        // 外国血统 <====> 外国血统中国籍人士 <===> 入籍
        // 其他  <====> 未定族称人口
        if (code == null) {
            if ("外国血统中国籍人士".contains(nationalName) || "入籍".equals(nationalName)) {
                nationalName = "外国血统";
            }
            if ("未定族称人口".contains(nationalName)) {
                nationalName = "其他";
            }
            if ("土".equals(nationalName)) {
                nationalName = "土家";
            }
            code =  CHINESE_NATIONALITY_CODE.getKey(nationalName);
        }
        // 补偿方案2，如果名族后面没有 加族，我们默认加族
        String suffix = "族";
        if (code == null) {
            if (!nationalName.contains(suffix)) {
                nationalName = nationalName + suffix;
                code =  CHINESE_NATIONALITY_CODE.getKey(nationalName);
            }
        }
        return code;
    }

    /**
     * 根据民族名称 获取编码,编码小于10，如：X---> 0X
     * @param nationalName 民族名称
     * @return 民族编码
     * @author dazer
     * @since 5.6.4
     */
    public static String getNationalityCodeStr(String nationalName) {
        Integer code = getNationalityCode(nationalName);
        if (code != null) {
            return StrUtil.padPre(code.toString(),2,"0");
        } else {
            return "01";
        }
    }
    // -----------------------------------------------------------------------------------  民族 nationality method end

}

